#include "Render2D.h"
#include "Config.h"
#include "Sprite.h"
#include "Tracer.h"
#include <allegro.h>
#include <vector>

Render2D::Render2D(void):
WIDTH(Config::WIDTH),
HEIGHT(Config::HEIGHT),
max_depth(0),
page_num(1)
{
	init();
	sprites.resize(8);

	sprites[0].resize(10);
	sprites[0].assign(10, NULL);

	sprites[1].reserve(2048);
	sprites[1].assign(2048, NULL);
	sprites[2].reserve(20);
	sprites[2].assign(20, NULL);
}

Render2D::~Render2D(void)
{
	destroy_bitmap(buffer);
	allegro_message("Juz koncze!");
	//release sprites
}

void Render2D::init()
{
	//install_keyboard();

	set_color_depth(32);

		//if (set_gfx_mode(GFX_AUTODETECT, WIDTH, HEIGHT, 0, 0) != 0)
		if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, WIDTH, HEIGHT, 0, 0) != 0)
		//if (set_gfx_mode(GFX_DIRECTX_ACCEL, WIDTH, HEIGHT, 0, 0) != 0)
		{
			allegro_message("Autodetect failed!");

			if (set_gfx_mode(GFX_SAFE, WIDTH, HEIGHT, 0, 0) != 0) 
			{
				set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
				allegro_message("Unable to set any graphic mode\n%s\n", allegro_error);
				//return 1;
			}
        }

		//install_mouse();
		//show_mouse(screen);

		//set_palette(desktop_palette);
		set_alpha_blender();

		buffer = create_bitmap(WIDTH, HEIGHT);
		//buffer = create_video_bitmap(WIDTH, HEIGHT);
		clear_bitmap(buffer);
		rectfill(buffer, 20, 20, WIDTH-20, HEIGHT-20, makeacol(255,0,0,255));

		//page[0] = create_video_bitmap(WIDTH, HEIGHT);
		//page[1] = create_video_bitmap(WIDTH, HEIGHT);
		
		int t = 0;
		sprites.resize(1);
}

void Render2D::drawFrame()
{
	unsigned int i = 0;
	unsigned int  n = 0;

	acquire_screen();
	clear_bitmap(buffer);
	//acquire_bitmap(page[0]);
	//clear_bitmap(page[0]);

	//currentPage = page[0];

	for(i = 0; i <= max_depth; i++)
	{
		for(n = 0; n < sprites[i].size(); n++)
		{
			if( sprites[i][n] == NULL)
			{
				//sprites[i].erase( sprites[i].begin() + n );
			}
			else
			{
				sprites[i][n]->draw();
				//Tracer::trace("drawn a sprite");
			}
		}
	}

	blit(buffer, screen,0,0,0,0,WIDTH,HEIGHT);
	//draw_trans_sprite(screen, buffer, 0, 0);
	release_screen();

	//blit(page[0], page[1],0,0,0,0,WIDTH,HEIGHT);
	//release_bitmap(page[0]);
	//show_video_bitmap(page[1]);
    //page_num = 1-page_num;
}

Sprite ** Render2D::addSprite(Sprite *s, unsigned int depth)
{
	
	if( depth > max_depth)
	{
		//sprites.resize( depth + 1);	
		/*for(unsigned int i = max_depth; i <= depth; ++i)
		{
			if(depth == 1)
			{
				//sprites[depth].reserve(2048);
				//sprites[depth].assign(2048, NULL);
				sprites[i].reserve(2048);
				sprites[i].assign(2048, NULL);
			}
			else
			{
				sprites[i].reserve(100);
				sprites[i].assign(100, NULL);
			}
		}*/

		max_depth = depth;
	}

	if( s->dest == NULL) s->dest = buffer;
	//if( s->dest == NULL) s->dest = page[0];

	//sprites[depth].push_back(s);
	std::vector <Sprite *>::iterator it;
	it = find(sprites[depth].begin(), sprites[depth].end(), (Sprite *)NULL);
	*it = s;
	
	return &*it;
}

void Render2D::drawText(char *t, unsigned int x, unsigned int y, FONT *f)
{
	textprintf_ex(buffer, font, x, y, makecol(180, 215, 65),
		    -1, "siema");
}
